/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import org.apache.log4j.Logger;

import who.tmap.bo.TextStyle;
import who.tmap.toolkit.shape.annotation.TitleShape;

/**
 * A special component to store the thematic map title
 * @author stl
 *
 */
public class TitleShapeImpl extends TextShapeImpl implements TitleShape {

    private static Logger logger = Logger.getLogger(TitleShapeImpl.class);	
	private boolean visible;
	private boolean backgroundTransparent;
	
	public TitleShapeImpl() {
		super();
		setType( TITLE );
		setPt( null );
		setBackgroundTransparent( false );
		visible =true;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	public boolean isBackgroundTransparent() {
		return backgroundTransparent;
	}

	public void setBackgroundTransparent(boolean backgroundTransparent) {
		this.backgroundTransparent = backgroundTransparent;
	}

	public void applyStyle(TextStyle textStyle) {
		setFontName( new String( textStyle.getFontName() ) );
		setFontSize( textStyle.getFontSize() );		
		setFontStyle( textStyle.getFontStyle() );
	}
	
	/**
	 * Draw the title
	 */
	public void draw(Graphics g) {  

		Graphics2D g2d =(Graphics2D)g;
		
		TextRenderingHelper trh =new TextRenderingHelper( getScreenToMapFactor(), getZoomFactor() );
		
		trh.init( g2d, fontName, fontStyle, fontSize );
		
		bounds =trh.prepareTextForRendering( g2d, getText(), pt, TextRenderingHelper.ADD_MARGINS );
		
		trh.renderText( g2d, backgroundTransparent, getBackgroundColor() );
		
		displaySelectionHandles( g );	
		
	}	
	
	public void moveShape(Point2D anchor, Point2D movingAnchor) {
		//super.moveShape( anchor, movingAnchor );
	    double offsetX =movingAnchor.getX() - anchor.getX();
	    double offsetY =movingAnchor.getY() - anchor.getY();
	    
		double origWidth =bounds.getWidth() / ( 1d + ( TextRenderingHelper.LEGEND_MARGIN * 2d ) );
		double origHeight =bounds.getHeight() / ( 1d + ( TextRenderingHelper.LEGEND_MARGIN * 2d ) );
		
		double widthMargin =( bounds.getWidth() - origWidth ) / 2d;
		double heightMargin =( bounds.getHeight() - origHeight ) / 2d;
		
	    pt.setLocation( pt.getX() + offsetX, pt.getY() + offsetY );	
		
		bounds.setFrame( //bounds.getMinX() - widthMargin, bounds.getMinY() - heightMargin
						 pt.getX() - widthMargin
					   , pt.getY() - heightMargin
					   , bounds.getWidth(), bounds.getHeight() );
	}	

}	
	
	

